package ren.kane.asdppp.classification;

import ren.kane.asdppp.Date;
import ren.kane.asdppp.transaction.cost.TimeCard;
import ren.kane.asdppp.transaction.pay.Paycheck;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 * @author renrk(1275) on 2019-04-12 10:24 AM
 **/
public class HourlyClassification implements PaymentClassification {

    private double hourlyRate;

    private Map<LocalDate, TimeCard> timeCardMap = new HashMap<>();

    public HourlyClassification() {
    }

    public HourlyClassification(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

    public double getHourlyRate() {
        return hourlyRate;
    }

    public TimeCard getTimeCard(LocalDate date) {
        return timeCardMap.get(date);
    }

    public void addTimeCard(TimeCard timeCard) {
        timeCardMap.put(timeCard.getDate(), timeCard);
    }

    @Override
    public double calculatePay(Paycheck paycheck) {
        double todayPay = 0;
        for (Map.Entry<LocalDate, TimeCard> entry : timeCardMap.entrySet()) {
            if (Date.isBetween(entry.getKey(), paycheck.getPayPeriodStartDate(), paycheck.getPayPeriodEndDate())) {
                todayPay += calculatePayForTimeCard(entry.getValue());
            }
        }
        return todayPay;
    }

    private double calculatePayForTimeCard(TimeCard timeCard) {
        double hours = timeCard.getHours();
        double overtime = Math.max(0.0, hours - 8.0);
        double straightTime = hours - overtime;
        return straightTime * hourlyRate + overtime * hourlyRate * 1.5;
    }
}
